﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Xceed.Document.NET;
using Xceed.Words.NET;

namespace DirectoryGen
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if(args.Length !=1) {
                Console.WriteLine("参数个数错误");
                Console.ReadKey();
                return;
            }
            //控制台程序获取当前运行路径 使用System.AppDomain.CurrentDomain.BaseDirectory
            string xmlpath =System.AppDomain.CurrentDomain.BaseDirectory+"/config.xml";
            //Winform程序获取当前运行路径 使用Environment.CurrentDirectory 
            string errorlog = System.AppDomain.CurrentDomain.BaseDirectory + "/error.xml";
            string modeldir;
            string targetdir;
            const string DIR_EDITFILE="编辑加工材料";
            const string DIR_ORGINFILE = "原稿";
            if(!File.Exists(xmlpath)) {
                Console.WriteLine("没有对应配置文件"+xmlpath);
                Console.ReadKey();
                return;
            }
            else
            {
                //读取配置文件信息
                XmlDocument xmlDocument = new XmlDocument();
                
                try
                {
                    xmlDocument.Load(xmlpath);
                    XmlNode root = xmlDocument.SelectSingleNode("root");
                    XmlNode modeldirNode = root.SelectSingleNode("modeldir");
                    XmlNode targetdirNode = root.SelectSingleNode("targetdir");
                    modeldir = modeldirNode.InnerText;
                    targetdir = targetdirNode.InnerText;
                    //复制原稿
                    DirectoryInfo directoryInfo = new DirectoryInfo(targetdir + "/" + DIR_ORGINFILE);
                    DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
                    List<int> nums = new List<int>();
                    foreach (DirectoryInfo directory in directoryInfos)
                    {
                        nums.Add(int.Parse(directory.Name.Split('.')[0]));
                    }
                    int nextnum = 0;
                    if (nums.Count == 0)
                        nextnum = 1;
                    else nextnum = nums.Max() + 1;
                    string targetdirpath = targetdir + "/" + DIR_ORGINFILE + "/" + nextnum.ToString() + "." + Path.GetFileNameWithoutExtension(args[0]);
                    Directory.CreateDirectory(targetdirpath);
                    Console.WriteLine(targetdirpath);
                    File.Copy(args[0], targetdirpath + "/" + Path.GetFileName(args[0]), true);
                    //复制模板
                    DirectoryInfo directoryInfoModel = new DirectoryInfo(modeldir);
                    FileInfo[]modelfilesinfo=directoryInfoModel.GetFiles();
                    string targeteditfilepath = targetdir + "/" + DIR_EDITFILE + "/" + nextnum.ToString() + "." + Path.GetFileNameWithoutExtension(args[0]);
                    Directory.CreateDirectory(targeteditfilepath);
                    Console.WriteLine(targeteditfilepath);
                    foreach (FileInfo file in modelfilesinfo)
                    {
                        string readpath = file.FullName;
                        string writepath = targeteditfilepath + "/" + Path.GetFileNameWithoutExtension(args[0]) + "  " + file.Name;

                        DocX docx = DocX.Load(readpath);
                        StringReplaceTextOptions srop=new StringReplaceTextOptions();
                        srop.SearchValue = "{{t1}}";
                        srop.NewValue = Path.GetFileNameWithoutExtension(args[0]);
                        docx.ReplaceText(srop);
                        docx.SaveAs(writepath);
                    }

                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message+"\n请查看错误日志");
                    File.WriteAllLines(errorlog,new string[] { ex.Message.ToString(),ex.StackTrace});
                    Console.ReadKey();
                }
                
            }
        }
    }
}
